#ifndef MESSAGE_H_
#define MESSAGE_H_


/*
 * @file    message.h
 *
 * Created on: March 10, 2012
 * Authors: Zeddy Chirombe, Alex Drake 
 *          {i.z.chirombe, alex.drake}@wustl.edu
 *
 * Message Class is the underlying fabric for our communication pattern between
 * all ROS nodes that are part of Robot Harris's make up, and are need to 
 * engage in voice aided Robot/Human interaction. It is a true replica to its,
 * java version running on Android phones and tablet.
 */


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>


using namespace std;

enum HarrisMsgTypes {
    //--------All android here -----------
    ANDROID_UNDEFINED=0,
    ANDROID_INTRO,          // Just a welcom aboad message
    ANDROID_SUCCESS,
    ANDROID_BROADCAST,      // one way voice message - 
    ANDROID_QUESTION,       // Question with replay - 
    ANDROID_GRID_LOCATION,  // Ask human for a desired map location - 
    ANDROID_BIOMETRICS,      // ask Android to generate biometric signature - 
    ANDROID_USER_LIMIT        // Android upper limit
    //--------All ROS here -------------

};

//---- Helper Functions ----
void parse_list(const string    list_str,
                vector<string>& recv,
                const char      sep,
                size_t          start = 0);

//---- Classes ----
/**
 * Message Class is the underlying fabric to our communication pattern between
 * the ROS and Android. 
 **/
class Message {
public:
	Message();
	Message(int);
	~Message();
    void initMsgTable();
    void reset();
    /**
     * Parse incoming messsage from the wire into this Message.
     * @param: Message from the peer(), 
     * @param: fromAndroid to correctly cipher MsgType.
     * @retval: 0 on success, -1 otherwize.
     **/
	int read(string message, bool fromAndroid);
    /**
     * return message size as field count.
     * @retval: total fields in this messages.
     **/
	int size() const;
    /**
     * return the string representaton of the entry on parts[index].
     * @retval: parts[index] on success, "" otherwise..
     **/
	string entry(unsigned int index) const; 
    /**
     * return message_type
     **/
	int message_type() const;
    /**
     * return message_priority
     **/
	int message_priority() const;
    /**
     * set message_type
     **/
	void message_type(int);
    /**
     * Append this string as a field to the back of the current message.
     **/
	void add_entry(string);
	void insert_entry(int, string);
    /**
     * Get this message as a string represenation. 
     **/
	string android_str() const;
	string str() const;
	string str(int start_pos) const;

    Message& operator=(Message& msg);
    //Overload the < operator.
    bool operator< (const Message& msg);
    //Overload the < operator.
    bool operator> (const Message& msg);

private:
	char separator;
	int MsgType;
	int MsgPriority;
	vector<string> parts;
	
	//---- Helper Functions ----
	map<string, int> str2int_map;
	map<int, string> int2str_map;

};



#endif
